package com.way.chapter01;

import java.util.concurrent.TimeUnit;

/**
 * @author 魏彪
 */
public class BlockedInterruptExample extends Thread {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().isInterrupted());
        while (!Thread.currentThread().isInterrupted()) {
            try {
                TimeUnit.SECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("线程已被中断");
    }

    public static void main(String[] args) throws InterruptedException {
        BlockedInterruptExample blockedInterruptExample = new BlockedInterruptExample();
        blockedInterruptExample.start();
        TimeUnit.SECONDS.sleep(100);
        System.out.println("before:InterruptExample 中断状态" + blockedInterruptExample.isInterrupted());
        blockedInterruptExample.interrupt();
        System.out.println("after:InterruptExample 中断状态" + blockedInterruptExample.isInterrupted());
    }
}
